package com.springboot.fram.core.collection;

import java.io.*;
import java.util.*;

/**
 * @author madman
 * @version 1.0
 * @date 2019/6/19 23:20
 */
public class CollectionUtils {
    /**
     * @param map
     * @return 将空字符转为null
     */
    public static Map<String, Object> emptyToNull(Map<String, Object> map) {
        Set<String> set = map.keySet();
        if (set != null && !set.isEmpty()) {
            for (String key : set) {
                if ("".equals(map.get(key))) {
                    map.put(key, null);
                }
            }
        }
        return map;
    }

    /**
     * @param map
     * @return 将null转为""
     */
    public static Map<String, Object> nullToEmpty(Map<String, Object> map) {
        Set<String> set = map.keySet();
        if (set != null && !set.isEmpty()) {
            for (String key : set) {
                if (map.get(key) == null) {
                    map.put(key, "");
                }
            }
        }
        return map;
    }

    /**
     * @author Madman 2018年5月8日
     * 将url参数按字典序列（ASCII码）排序
     */
    public static <K, V extends Comparable<? super V>> String buildSignAscii(Map<K, V> params) {
        StringBuilder sb = new StringBuilder();
        // 将参数以参数名的字典升序排序
        Map<K, V> sortParams = new TreeMap<K, V>(params);
        // 遍历排序的字典,并拼接"key=value"格式
        for (Map.Entry<K, V> entry : sortParams.entrySet()) {
            if (entry.getValue() != null) {
                if (sb.length() != 0) {
                    sb.append("&");
                }
                sb.append(entry.getKey()).append("=").append(entry.getValue());
            }
        }
        return sb.toString();
    }


    /**
     * 通过HashSet踢除重复元素
     *
     * @param list
     * @return
     */
    public static <T> List<T> removeDuplicate(List<T> list) {
        HashSet hashSet = new HashSet(list);
        list.clear();
        list.addAll(hashSet);
        return list;
    }


    /**
     * 集合复制
     * Collections.copy()
     * clone等方法均可
     *
     * @param srcList
     * @param <T>
     * @return
     */
    public static <T> List<T> listCopy(List<T> srcList) {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(srcList);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream inStream = new ObjectInputStream(byteIn);
            List<T> destList = (List<T>) inStream.readObject();
            return destList;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }


    /**
     * 开始分页
     *
     * @param list     需要分頁的集合
     * @param pageNum  页码
     * @param pageSize 每页多少条数据
     * @return
     */
    public static <T> List<T> startPage(List<T> list, Integer pageNum,
                                        Integer pageSize) {
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        // 记录总数
        int count = list.size();
        // 页数
        int pageCount = 0;
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        // 开始索引
        int fromIndex = 0;
        // 结束索引
        int toIndex = 0;

        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }

        List<T> pageList = list.subList(fromIndex, toIndex);

        return pageList;
    }

}
